package com.ugoodtech.mdcc.web.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.domain.QQuestionSection;
import com.ugoodtech.mdcc.core.domain.QuestionPaper;
import com.ugoodtech.mdcc.core.domain.QuestionSection;
import com.ugoodtech.mdcc.core.repository.QuestionPaperRepository;
import com.ugoodtech.mdcc.core.repository.QuestionSectionRepository;
import com.ugoodtech.mdcc.web.service.QuestionSectionService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class QuestionSectionServiceImpl implements QuestionSectionService {

    @Autowired
    private QuestionSectionRepository questionSectionRepository;

    @Autowired
    private QuestionPaperRepository questionPaperRepository;


    @Override
    public Page<QuestionSection> queryQuestionSection(Long paperId, Pageable pageable) {
        QQuestionSection qQuestionSection = QQuestionSection.questionSection;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionSection.deleted.eq(false));
        if (null!=paperId) {
            builder.and(qQuestionSection.paper.id.eq(paperId));
        }
        Page<QuestionSection> questionSections = questionSectionRepository.findAll(builder,pageable);
        return questionSections;
    }

    @Override
    public QuestionSection createQuestionSection(@ApiParam("大题名称")  String name,
                                                 @ApiParam("问卷Id")  Long paperId) {
        QuestionSection questionSection=new QuestionSection();
        QuestionPaper questionPaper= questionPaperRepository.findOne(paperId);
        questionSection.setPaper(questionPaper);
        questionSection.setName(name);
        questionSection=questionSectionRepository.save(questionSection);
        questionSection.setIdx(questionPaper.getId().intValue());
        return questionSectionRepository.save(questionSection);
    }
}
